//
//  commonTools.swift
//  微博
//
//  Created by 范承志 on 16/6/11.
//  Copyright © 2016年 fanchengzhi. All rights reserved.
//

import UIKit
import AFNetworking
// get post 请求方式枚举(Swift 枚举写法)
enum CZNetworkToolsMethod: String {
    case get = "get"
    case post = "post"
}

class CZNetworkTools: AFHTTPSessionManager {
    // 全局访问点
    static let sharedTools: CZNetworkTools = {
        let tools = CZNetworkTools()
        // 反序列化格式
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    /// 网络请求公共方法
    ///
    /// - parameter method:     请求方式
    /// - parameter urlString:   url 地址
    /// - parameter parameters: 请求参数
    /// - parameter success:    成功
    /// - parameter failure:    失败
    func requet(method: CZNetworkToolsMethod, urlString: String, parameters: Any?, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 如果是 get 请求
        if method == .get {
            get(urlString, parameters: parameters, progress: nil, success: { (_, responseObject) in
                success(responseObject)
                }, failure: { (_, error) in
                    failure(error)
            })
        }else {
            // post 请求
            post(urlString, parameters: parameters, progress: nil, success: { (_, responseObject) in
                success(responseObject)
                }, failure: { (_, error) in
                    failure(error)
            })
        }
        
    }
}

// MARK: - 首页相关接口
extension CZNetworkTools {
    
    func HomeLoadData(sinceId: Int64, maxId: Int64, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        
        //请求url
        let urtString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        //请求参数
        let params: [String : Any] = [
            "access_token": CZUserAccountViewModel.sharedTools.accessToken!,
            "since_id": sinceId,
            "max_id": maxId
        ]
    
        //发送请求
        requet(method: CZNetworkToolsMethod.get, urlString: urtString, parameters: params, success: success, failure: failure)
    }
}

// MARK: - OAuth 授权相关接口
extension CZNetworkTools {
    // 获取 userAccount
    func oauthLoadUserAccount(code: String, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 请求 urlString
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        // 请求参数
        let params = [
            "client_id": CZAppKey,
            "client_secret": CZAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": CZAppHuiDiaoYe
        ]
        
        // 发送请求
        requet(method: CZNetworkToolsMethod.post, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
        //请求用户信息
        func oauthLoadUserInfo(model: CZUserAccountModel,success:@escaping (Any?)->(), failure:@escaping(Error)->()){
            
            //请求urlString
            let urlString = "https://api.weibo.com/2/users/show.json"
            
            //请求参数
            let params = [
                "access_token": model.access_token!,
                "uid": model.uid!
            ]
        
            //发送请求
            requet(method: CZNetworkToolsMethod.get, urlString: urlString, parameters: params, success: success, failure: failure)
        }
    }
